const express = require('express');
const router = express.Router();
const db = require("../../../../util/dbConfig/dbConfig")

router.post('/',async (req,res) => {
    if(req.session.userInfo.admin !== 1){
        return res.send({
            code: 3,
            msg: '非管理员不可添加'
        })
    }
    let {
        sheepId,  //羊只编号
        level,     //等级
        year,  //年度
        bodyHight,   //体高
        bodyLength,    //体长
        bust,        //胸围
        bodyWeight,  //体重
        pileLength,     //绒长度
        pileWide,       //绒细度
        pileYield,       //绒产量
        tube,            //管围
        pileThickness,      //绒厚
        remake              //备注
    }=req.body

    let arr = [
        year,  //年度
        sheepId,  //羊只编号
        bodyHight,   //体高
        bodyLength,    //体长
        bust,        //胸围
        bodyWeight,  //体重
        pileLength,     //绒长度
        pileWide,       //绒细度
        pileYield,       //绒产量
        level,     //等级
        tube,            //管围
        pileThickness,      //绒厚
        remake              //备注
    ]  //将req.body中的数据封装成数组

    if(arr.length !== 13){
        return res.send({
            code: 1,
            msg: '参数错误'
        })
    }
    try{
        //按羊只编号查询是否有该用户
        let findSql = `select USER_ID from sheep where ID=?`
        let findSqlArr = [sheepId]
        let result1 = await db.SySqlConnect(findSql,findSqlArr)
        if(result1.length === 0){
            return res.send({
                code: 1,
                msg: '该羊只无牧户或无该羊信息，请确定该羊只编号是否存在'
            })
        }else if(result1.length === 1){
            //该羊有牧户
            let flag = 0  //如果为1说明年份重复
            //查询该羊本年度是否有数据，避免重复
            let yearSql = `select YEAR from production where SHEEP_ID=?`
            let yearSqlArr = [sheepId]
            let result2 = await db.SySqlConnect(yearSql,yearSqlArr)
            if(result2.length !== 0){
                let yearClientSend = Number(year.slice(0,4))   //获取数据库中的年份索引
                for(let i = 0 ; i < result2.length ; i++){
                    let yearStr = result2[i].YEAR.getFullYear()
                    if(yearStr === yearClientSend){
                        flag = 1  //年份重复
                        return res.send({
                            code: 2,
                            msg: '有重复年份'
                        })
                    }
                }
            }
            //无重复年份 或者无年度数据
            if(result2.length ===0 || flag === 0){
                let addSql = `insert into production
        (YEAR,
        SHEEP_ID,
        BODYTALL,
        BODYLONG,
        CHEST,
        WEIGHT,
        LENGTH,
        FINE,
        RONGYIELD,
        TANK,
        PIPE,
        TINCK_WOOL,
        REMARK) values(?,?,?,?,?,?,?,?,?,?,?,?,?)`
                let result = await db.SySqlConnect(addSql,arr)
                if(result.affectedRows === 1){
                    return res.send({
                        code: 0,
                        msg: '添加成功'
                    })
                }else{
                    return res.send({
                        code: 2,
                        msg: '添加失败'
                    })
                }
            }

        }else{
            console.error("production can not run , please send err to wjf ")
            return res.send({
                code: 4,
                msg: '服务器错误,请稍后再试'
            })
        }


    }catch (e) {
        console.log(e);
        return res.send({
            code: 4,
            msg: '服务器错误,请稍后再试'
        })
    }
})

module.exports = router
